<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/30
 * Time: 17:11
 */

namespace app\applet\model;


use Stbz\Api\SupplyClient;
use think\Db;

class SupplyNotify
{
    private $appKey   = 'A855F0BA1EBCD119BEFBDE6EC07ECE65';
    private $appSecret = '68B892679EB78F9FB45ED3D62DE838B7';

    public function __construct()
    {
        vendor("JuHe.autoload");
    }
    #商品下架
    public function undercarriage($goods_id)
    {
        $count = Db::name("choice_goods")
            ->where(['goods_id'=>$goods_id])
            ->count();
        if($count)
        {
            $data['status'] = 0;
            Db::name("choice_goods")
                ->where(['goods_id'=>$goods_id])
                ->update($data);
        }
    }
    #商品上架
    public function goodsOnSale($goods_id)
    {
        $count = Db::name("choice_goods")
            ->where(['goods_id'=>$goods_id])
            ->count();
        if($count)
        {
            $data['status'] = 1;
            Db::name("choice_goods")
                ->where(['goods_id'=>$goods_id])
                ->update($data);
        }

    }
    #移除商品库商品
    public function goodsRemove($goods_id)
    {
        $count = Db::name("choice_goods")
            ->where(['goods_id'=>$goods_id])
            ->count();
        if($count)
        {
            $data['supply_delete_status'] = 0;
            Db::name("choice_goods")
                ->where(['goods_id'=>$goods_id])
                ->update($data);
        }
    }
    #商品修改
    public function goodsAlter($goods_id)
    {
        $this->client = new SupplyClient( $this->appKey, $this->appSecret);
        $param['id'] = $goods_id;
        $method = 'GET';//请求方法
        $action = '/v2/Goods/Detail';//请求资源名
        $response = $this->client->getApiResponse($method,$action,$param);
        $response = json_decode($response,true);
        #判断商品id是否在选品库存在
        if($response['code'] === 1)
        {
            $count = Db::name("choice_goods")
                ->where(['goods_id'=>$goods_id])
                ->count();
            #判断商品是否已经同步本地数据库
            if($count)
            {
                $response = $response['data'];
//                $data['goods_id'] = $goods_id;
                $data['title'] = $response['title'];
                $data['brand_name'] = empty($response['third_brand_name']) ? "其他" : $response['third_brand_name'];
                $data['source'] = $response['source'];
                $data['market_price'] = $response['market_price'];
                $data['agreement_price'] = $response['agreement_price'];
                $data['cover'] = $response['cover'];
                $data['stock'] = $response['stock'];
                $data['total_stock'] = $response['total_stock'];
                $data['status'] = $response['status'];
                $data['sale'] = $response['sale'];
                $data['is_free_shipping'] = $response['is_free_shipping'];
                $data['update_time'] = time();
                Db::name("choice_goods")
                    ->where(['goods_id'=>$goods_id])
                    ->update($data);
            }
        }

    }
    #消息推送添加商品
    public function goodsAdd($goods_id)
    {
        $this->client = new SupplyClient( $this->appKey, $this->appSecret);
        $param['id'] = $goods_id;
        $method = 'GET';//请求方法
        $action = '/v2/Goods/Detail';//请求资源名
        $response = $this->client->getApiResponse($method,$action,$param);
        $response = json_decode($response,true);
        #判断商品id是否在选品库存在
        if($response['code'] === 1)
        {
            $count = Db::name("choice_goods")
                ->where(['goods_id'=>$goods_id])
                ->count();
            #判断商品是否已经同步本地数据库
            $response = $response['data'];
            if(!$count)
            {
                $data['goods_id'] = $goods_id;
                $data['title'] = $response['title'];
                $data['brand_name'] = empty($response['third_brand_name']) ? "其他" : $response['third_brand_name'];
                $data['source'] = $response['source'];
                $data['market_price'] = $response['market_price'];
                $data['agreement_price'] = $response['agreement_price'];
                $data['cover'] = $response['cover'];
                $data['stock'] = $response['stock'];
                $data['total_stock'] = $response['total_stock'];
                $data['status'] = $response['status'];
                $data['sale'] = $response['sale'];
                $data['is_free_shipping'] = $response['is_free_shipping'];
                $data['create_time'] = time();
                Db::name("choice_goods")->insert($data);
            }else{
                $data['title'] = $response['title'];
                $data['brand_name'] = empty($response['third_brand_name']) ? "其他" : $response['third_brand_name'];
                $data['source'] = $response['source'];
                $data['market_price'] = $response['market_price'];
                $data['agreement_price'] = $response['agreement_price'];
                $data['cover'] = $response['cover'];
                $data['stock'] = $response['stock'];
                $data['total_stock'] = $response['total_stock'];
                $data['status'] = $response['status'];
                $data['sale'] = $response['sale'];
                $data['is_free_shipping'] = $response['is_free_shipping'];
                $data['update_time'] = time();
                $data['supply_delete_status'] = 1; #添加更新回来
                Db::name("choice_goods")
                    ->where(['goods_id'=>$goods_id])
                    ->update($data);
            }
        }


    }

}